package org.nnxy.foodboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.nnxy.foodboot.entity.RecipeFeature;
import org.nnxy.foodboot.mapper.RecipeFeatureMapper;
import org.nnxy.foodboot.service.RecipeFeatureService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 食谱特征Service实现类
 */
@Service
@RequiredArgsConstructor
public class RecipeFeatureServiceImpl extends ServiceImpl<RecipeFeatureMapper, RecipeFeature> implements RecipeFeatureService {
    
    @Override
    public RecipeFeature getRecipeFeatureByRecipeId(Integer recipeId) {
        if (recipeId == null) {
            return null;
        }
        
        LambdaQueryWrapper<RecipeFeature> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RecipeFeature::getRecipeId, recipeId);
        
        return getOne(queryWrapper);
    }
    
    @Override
    public boolean updateRecipeFeature(RecipeFeature recipeFeature) {
        if (recipeFeature == null || recipeFeature.getRecipeId() == null) {
            return false;
        }
        
        RecipeFeature existingFeature = getRecipeFeatureByRecipeId(recipeFeature.getRecipeId());
        
        if (existingFeature != null) {
            recipeFeature.setId(existingFeature.getId());
            return updateById(recipeFeature);
        }
        
        return false;
    }
    
    @Override
    public boolean createRecipeFeature(RecipeFeature recipeFeature) {
        if (recipeFeature == null || recipeFeature.getRecipeId() == null) {
            return false;
        }
        
        RecipeFeature existingFeature = getRecipeFeatureByRecipeId(recipeFeature.getRecipeId());
        
        if (existingFeature == null) {
            return save(recipeFeature);
        }
        
        return false;
    }
    
    @Override
    public List<Integer> getRecipeIdsByHealthTag(String healthTag, Integer limit) {
        if (healthTag == null || healthTag.isEmpty() || limit == null || limit <= 0) {
            return new ArrayList<>();
        }
        
        LambdaQueryWrapper<RecipeFeature> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(RecipeFeature::getHealthTags, healthTag)
                .orderByDesc(RecipeFeature::getCreatedTime)
                .last("LIMIT " + limit);
        
        List<RecipeFeature> features = list(queryWrapper);
        List<Integer> recipeIds = new ArrayList<>();
        for (RecipeFeature feature : features) {
            recipeIds.add(feature.getRecipeId());
        }
        
        return recipeIds;
    }
    
    @Override
    public List<Integer> getRecipeIdsByTasteProfile(String tasteProfile, Integer limit) {
        if (tasteProfile == null || tasteProfile.isEmpty() || limit == null || limit <= 0) {
            return new ArrayList<>();
        }
        
        LambdaQueryWrapper<RecipeFeature> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(RecipeFeature::getTasteProfile, tasteProfile)
                .orderByDesc(RecipeFeature::getCreatedTime)
                .last("LIMIT " + limit);
        
        List<RecipeFeature> features = list(queryWrapper);
        List<Integer> recipeIds = new ArrayList<>();
        for (RecipeFeature feature : features) {
            recipeIds.add(feature.getRecipeId());
        }
        
        return recipeIds;
    }
    
    @Override
    public List<Integer> getRecipeIdsByCuisineType(String cuisineType, Integer limit) {
        if (cuisineType == null || cuisineType.isEmpty() || limit == null || limit <= 0) {
            return new ArrayList<>();
        }
        
        LambdaQueryWrapper<RecipeFeature> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RecipeFeature::getCuisineType, cuisineType)
                .orderByDesc(RecipeFeature::getCreatedTime)
                .last("LIMIT " + limit);
        
        List<RecipeFeature> features = list(queryWrapper);
        List<Integer> recipeIds = new ArrayList<>();
        for (RecipeFeature feature : features) {
            recipeIds.add(feature.getRecipeId());
        }
        
        return recipeIds;
    }
} 